/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DatabaseConnection;

import Entities.Route;
import Entities.Seat;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author serhat
 */
public class SeatDbConnection extends Connector {

    public SeatDbConnection() throws ClassNotFoundException,
            SQLException, InstantiationException, IllegalAccessException {
    }

    private String buildSelectBusSeatsQuery(Route route) {
        String query = String.format(" select * from KoltukDurumu where baslangicDurak"
                + " in (select durakNo from Durak where sefer = '%d') "
                + "and ('%s' between baslangicDurak"
                + " and sonDurak or '%s' between baslangicDurak and sonDurak )"
                + " and '%s' < sonDurak and '%s' > baslangicDurak",
                route.getId(), route.getStart(), route.getEnd(), route.getStart(), route.getEnd());
        System.out.println(query);
        return query;
    }

    public ResultSet SelectBusSeats(Route route) {
        try {
            String query = buildSelectBusSeatsQuery(route);
            ResultSet rs = executeQuery(query);
            return rs;
        } catch (SQLException ex) {
            Logger.getLogger(SeatDbConnection.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
    private String buildInsertQuery(Seat seat){
        String query = String.format("INSERT INTO KoltukDurumu "
                + "values(null, '%d', '%s', '%s', '%s', '%s')", 
                seat.getSeatNo(),
                seat.getRoute().getStart(),
                seat.getRoute().getEnd(),
                seat.getState(),
                seat.getGender());
        System.out.println(query);
        return query;
    }
    
    public int insert(Seat seat) {
        try {
            String query = buildInsertQuery(seat);
            int key = executeUpdateAndReturnKey(query);
            return key;
        } catch (SQLException ex) {
            Logger.getLogger(StopPointDbConnection.class.getName()).log(Level.SEVERE, null, ex);
            return -1;
        }

    }
}
